Class Nur.MNISV3.Service.Logon Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 记录PDA上传的日志
ClassMethod Log(method = "", text = "", userId = "", version = "")
{
   i $g(^PDALog("num"))=10000 k ^PDALog //1万条 清空
   s ^PDALog("num")=$g(^PDALog("num"))+1
   s ^PDALog(+$h,method_"-"_version,userId_"-",$zd(+$h,3)_"-"_$zt($p($h,",",2)))=$lb(text)
   q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("Log","010199","0")
}

/// Creator:      fengliang
/// CreatDate:    2020-07-17
/// Description:  获取用户的登录病区，如果logonWardId登录病区id不为空，判断用户是否
/// Input：       userCode 工号，password 密码，logonLocType 科室类型，logonWardId 登录病区id
/// Output：      用户登录科室信息
/// Others：       w ##class(Nur.MNISV3.Service.Logon).Logon("456","1","","","621")
ClassMethod Logon(userCode As %String, password As %String, logonLocType As %String = "W", userName As %String = "", locId = "") As %String
{
    n (userCode,password,logonLocType,userName,locId,%session)
    s ^MML("LogonV3")=$lb(userCode,password,logonLocType,userName,locId)
    s $zt="errLogon"
    s userCode=$zcvt(userCode,"u")
    s msg="",msgcode="999999",status="0" //定义初始值
    
    i userName'=""&&(userName'=$c(0))  d  //扫码登录
    .s userId=$o(^SSU("SSUSR",0,"SSUSR_Name",userName,""))
    e  d
    .i (password=""||(password=$c(0)))&&(userCode'="") d  //扫码登录
    ..s userId=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode),""))
    .e  d
    ..s userId=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode),""))
    ..s validUser=##class(web.SSUser).IsValidUser(userCode,password)
    
    i $g(validUser)["^" d
    .s errCode=$P(validUser,"^",1),errInfo=$P(validUser,"^",2)
    .s status="-1"
    .i errInfo="UserNotFindErr" s msg="无此用户",msgcode="010101"
    .i errInfo="UnActiveErr" s msg="用户未激活",msgcode="010102"
    .i errInfo="DateFromScopeErr" s msg="未到激活日期",msgcode="010103"
    .i errInfo="DateToScopeErr" s msg="用户已过期",msgcode="010104"
    .i errInfo="PasswordErr" s msg="密码错误",msgcode="010105"
    i userId="" s msg="无此用户",msgcode="010106"

    //取登录科室列表
    i userId'="" s locJson=##class(Nur.MNISV3.Service.UserCenter).GetUserLoc(userId,logonLocType)
    i ($g(msg)="")&&(locJson.Count()=0) d
    .s msg="没有可登陆的科室",msgcode="010107",status="-1"
    
    w ##class(Nur.MNISV3.Common.JSON).Encode(locJson),!
    //取默认登录科室
    i userId'="" s defLocInfo=##class(Nur.MNISV3.Service.UserCenter).GetUserDefLoc(userId,logonLocType)
    s usname=$p(defLocInfo,"^",1)
    s deflocId=$p(defLocInfo,"^",2)
    s deflocDesc=$p(defLocInfo,"^",3)
    s defgroupId=$p(defLocInfo,"^",4)
    s defgroupDesc=$p(defLocInfo,"^",5)
    
    //CA 相关用户 id(即 COSS 为用户生成的 MSSPID，该值与姓名身份 证号码相对应)
    s caUserId="bbd6afffc9da0e4e053f35644db6a4fd6cabd47be613298cec8ab8c60a85e17b"  //$list(^JYW("saveCaUserId"),2)
    s mobile="" //手机号（可选）
    s idNumber="130102196303250459" //身份证号
    i userCode="" s userCode=$p(^SSU("SSUSR",userId),"^",1) 
    s userCode=$$ALPHAUP^SSUTIL4(userCode)  //$zcvt(userCode,"u")
    i (locId'="") s caFlag=##class(Nur.MNISV3.Service.CASign).GetCAServiceStatus(locId,userId)
    
    //扫码广播信息
    s broadcastList=##class(Nur.MNISV3.Common.JSON).Decode(##class(Nur.MNIS.Service.Config).GetBroadcastConfig())
    
    //医嘱查询时间设置
    s SchDateTime=##class(Nur.MNISV3.Common.Utils).DelSchDateTime(userId)
    s stdatetime=$replace($P(SchDateTime,"^",1),","," ")
    s enddatetime=$replace($P(SchDateTime,"^",2),","," ")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))   
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(locJson),"locs")
    d json.SetAt(userId,"userId")
    d json.SetAt($g(usname),"userName")
    d json.SetAt(userCode,"userCode")
    d json.SetAt(broadcastList.GetAt("broadcastList"),"broadcastList")  
    d json.SetAt($g(curDateTime),"curDateTime")
    d json.SetAt($g(stdatetime),"schStDateTime")  
    d json.SetAt($g(enddatetime),"schEnDateTime")  
    d json.SetAt(caUserId,"caUserId") 
    d json.SetAt(mobile,"mobile") 
    d json.SetAt(idNumber,"idNumber")
    
    //配置
    d json.SetAt("1","bloodTransFusionScanFlag") //血液输注-默认"" 关闭, "1"开启不校验密码
    d json.SetAt("1","logFlag")  //输出Log日志: 默认是"" 不开启; "1" 开启
    d json.SetAt("1","caBatchCode") //CA支持批量: 默认是"" 不开启; "1" 开启
    //d json.SetAt("1","voiceFlag")  //是否开启语音
    s loginFlags=##class(Nur.MNIS.Service.Config).GetLoginFlags()
    s voiceFlag=loginFlags.GetAt("voiceFlag")
    s msgFlag=loginFlags.GetAt("msgFlag")
    d json.SetAt(voiceFlag,"voiceFlag")
    d json.SetAt(msgFlag,"msgFlag")
    d json.SetAt($g(caFlag),"caFlag")
    ;d json.SetAt("1","caFlag")
    ;d json.SetAt("1","pinFlag")
    q json
errLogon
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"010199","-1")
}

/// 密码验证
/// w ##class(Nur.MNISV3.Service.Logon).ValidateUserV4()
ClassMethod ValidateUserV4(userId, userCode, password, passFlag = "1")
{
    n (userId, userCode, password, passFlag)
    s ^MML("ValidateUserV4")=$lb(userId,userCode,password,passFlag)
    s $zt="errValidateUserV4"
    s msg="成功",msgcode=99999,status=0
    s userIdTemp=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode),""))
    i userIdTemp'=userId {
        i passFlag=1  ///登录密码
        {
            s userId=##class(web.SSUser).IsValidUser(userCode,password)
            i userId["^" d
            .s errInfo=$P(userId,"^",2)
            .s status="-1"
            .i errInfo="UserNotFindErr" s msg="无此用户",msgcode="010101",status="-1"
            .i errInfo="UnActiveErr" s msg="用户未激活",msgcode="010102",status="-1"
            .i errInfo="DateFromScopeErr" s msg="未到激活日期",msgcode="010103",status="-1"
            .i errInfo="DateToScopeErr" s msg="用户已过期",msgcode="010104",status="-1"
            .i errInfo="PasswordErr" s msg="密码错误",msgcode="010105",status="-1"
            i userId="" s msg="无此用户",msgcode="010106",status="-1"       
        }
        else ///签名密码
        {
            s userCode=$zcvt(userCode,"u")
            s userId=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode),""))
            i userId="" s msg="无此用户",msgcode="100000",status="-1"
            i userId'="" d
            .s passFlag=##Class(web.DHCOEOrdItem).PinNumberValid(userId,password) 
            .i passFlag'=0 s msg="密码错误",msgcode="100000",status="-1"
        }
        i (status="-1")&&(userId'="") {
            s userCode=$p($g(^SSU("SSUSR",userId)),"^",1)
            s userName=$p($g(^SSU("SSUSR",userId)),"^",2)
            s displayText=userName,convertedValue="CA"_userName_"*"_userCode
            s text=userCode
        }
        s json=##class(%ArrayOfDataTypes).%New()
        d json.SetAt(msg,"msg")
        d json.SetAt(msgcode,"msgcode")
        d json.SetAt(status,"status")
        d json.SetAt($g(text),"text")
        d json.SetAt($g(displayText),"displayText")
        d json.SetAt($g(convertedValue),"convertedValue")
        //i +userId=0 q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(userId,"1","0")
        q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    }
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("成功","0","0")
errValidateUserV4
    s msg=$ze,msgcode=199999,status=-1
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 文本框响应事件---Code默认user  text输入的工号
/// w ##class(Nur.MNISV3.Service.Logon).EditTextConvertV4("21","10.1.21.123","13")
ClassMethod EditTextConvertV4(code = "", text = "", event = "", userId = "") As %String
{
    n (code,text,event)
    s ^GRS("EditTextConvertV4")=$lb(code,text,event)
    s $zt="EditTextConvertV4Err"
    s msg="成功",msgcode=99999,status=0
    s displayText="",convertedValue=""
    i (text'="") {
        i (code="user") {
            s userId=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(text),""))
            i (userId="") s userId=$o(^SSU("SSUSR",0,"SSUSR_Name",$$ALPHAUP^SSUTIL4(text),""))
            i (userId'="") {
                s userCode=$p($g(^SSU("SSUSR",userId)),"^",1)
                s userName=$p($g(^SSU("SSUSR",userId)),"^",2)
                s displayText=userName,convertedValue="CA"_userName_"*"_userCode
                i (event="add")&(userCode'="") s text=userCode
            }
        }
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(text,"text")
    d json.SetAt(displayText,"displayText")
    d json.SetAt(convertedValue,"convertedValue")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
EditTextConvertV4Err
    s msg=$ze,msgcode=199999,status=-1
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      fengliang
/// CreatDate:    2020-07-17
/// Description:  获取新版本号
/// Input：       wardId 病区id,ip 服务器地址,curVersion 当前版本号
/// Output：      是否需要更新，如果需要返回更新的版本及下载地址
/// Others：      w ##class(Nur.MNISV3.Service.Logon).GetNewVersion("21","10.1.21.123","13")
ClassMethod GetNewVersion(wardId As %String, ip As %String, curVersion As %String)
{
    n (wardId,ip,curVersion)
    ;s ^EH("GetNewVersion")=$lb(wardId,ip,curVersion)
    s $zt="errGetNewVersion"
    s msg="",msgcode="999999",status="0" //定义初始值
    /*i wardId="" s updateInfo=""
    e  s updateInfo=##class(Nur.MNISV3.Common.Utils).GetNewVerion(wardId,ip,curVersion)
    s newVersion="",appAddress=""
    i updateInfo'=""
    {
        s newVersion=$P(updateInfo,"^",1)
        s appAddress=$P(updateInfo,"^",2)
    }*/
    s wardLocID=$p($g(^PAWARD(wardId)),"^",5) // EH
    s update=##class(Nur.HISUI.MNIS.Config).GetUpdate("",wardLocID)
    i (update.GetAt("ActiveFlag")="Y") d
    .s newVersion=update.GetAt("Version")
    .s appAddress=update.GetAt("DownloadLink")
    .s intip=ip,http="https",directory="/imedical/web/",file="MobileNurse.apk"
    .i (intip["://") s intip=$p(ip,"://",2),http=$p(ip,"://",1)
    .i (intip["/") s intip=$p(intip,"/",1)
    .i (appAddress["127.0.0.1") s appAddress=$replace(appAddress,"127.0.0.1",intip)
    .i (appAddress["/") s directory=""
    .i (appAddress[".apk") s file=""
    .i (appAddress["://") s http=""
    .s appAddress=http_appAddress_directory_file
    b
    if ($g(newVersion)="") s newVersion="-1"
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(newVersion),"newVersion")
    d json.SetAt($g(appAddress),"appAddress")
    d json.SetAt("7","newVersion")
    //d json.SetAt("http://192.168.110.155/NursePro-1.0.6.hap","appAddress")
    //d json.SetAt("http://192.168.110.155/NursePro-1.0.7.hap","appAddress")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetNewVersion
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"010299","-1")
}

/// Creator:      fengliang
/// CreatDate:    2020-07-17
/// Description:  设置查询时间
ClassMethod SetSchDateTime(userId As %String, stdatetime As %String, enddatetime As %String)
{
    n (userId,stdatetime,enddatetime)
    s $zt="errorSetSchDateTime"
    s msg="",msgcode="999999",Status="0" //定义初始值
    d ##class(Nur.MNISV3.Common.Utils).SetSchDateTime(userId,stdatetime,enddatetime)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errorSetSchDateTime
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"010399","-1")
}

/// 扫描工号牌是调用,CA扫码登录
/// w ##class(Nur.MNISV3.Service.Logon).Scanlogon("护士01")
ClassMethod Scanlogon(userName As %String, logonLocType As %String = "W", userId = "") As %String
{
    n (userName,logonLocType,userId)
    s ^MML("Scanlogon")=$LB(userName , logonLocType,userId)
    s $zt="errScanlogon"
    s msg="",msgcode="999999",status="0" //定义初始值
    s user=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userName),""))
    i $g(user)="" s user=$o(^SSU("SSUSR",0,"SSUSR_Name",userName,""))
    i ($g(user)'="")&&(userId="") s userId=user
    i $g(userId)="" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("用户不存在!","010299","-1")
    s json=##class(%ArrayOfDataTypes).%New()
    i userId<0 d
    .s errorInfo=$s(userId="-100":"用户不存在",userId="-101":"密码不正确",userId="-102":"用户没有激活",1:"")
    e  s usname=$p(^SSU("SSUSR",userId),"^",2) //by pan
    s locJson=##class(Nur.MNIS.Service.UserCenter).GetUserLoc(userId,logonLocType)
    d json.SetAt($g(errorInfo),"ErrorInfo")
    d json.SetAt($g(locJson),"Locs")
    d json.SetAt(userId,"UserID")
    i $g(userCode)="" s userCode=$p(^SSU("SSUSR",userId),"^",1) 
    s userCode=$$ALPHAUP^SSUTIL4(userCode)
    d json.SetAt($g(userCode),"userCode")
    d json.SetAt($g(usname),"UserName")  //by pan
    i ($g(errorInfo)="")&&(locJson.Count()=0) d
    .s errorInfo="用户没有可登陆的病区",status="-1"
    .d json.SetAt($g(errorInfo),"ErrorInfo")
     d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errScanlogon
    s error=$ze
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(error),"ErrorInfo")
    d json.SetAt("-1","status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
}

}
